package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.User;
import dataaccess.ContatoDao;

public class LoginServlet extends HttpServlet {
     protected void doPost(HttpServletRequest request,
             HttpServletResponse response)
             throws IOException, ServletException {

         PrintWriter out = response.getWriter();
                         
         // pegando os parâmetros do request
         String login = request.getParameter("login");
         String pwd = request.getParameter("pwd");
         
         ContatoDao dao = new ContatoDao();
         User contato = dao.getLista(login, pwd);
         if (contato==null){
        	 out.println("<html>");
             out.println("<body>");
             out.println("Senha inválida. <a href= 'index.html'>Retorne</a>");
             out.println("</body>");
             out.println("</html>");
         }
         else{
        	 out.println("<html>");
             out.println("<body>");
             out.println("Logado, aguarde...");
             out.println("</body>");
             out.println("</html>");
             //response.sendRedirect("member.jsp");
             HttpSession sessao = request.getSession();
             sessao.setAttribute( "usuario", login );
             sessao.setAttribute( "id", contato.getId() );
             
            RequestDispatcher rd = request.getRequestDispatcher("/member.jsp");				
     		request.setAttribute("erro", " ");     		
     		rd.forward(request, response);
         }
     }
     protected void doGet(HttpServletRequest request,
             HttpServletResponse response)
             throws IOException, ServletException {
    	 PrintWriter out = response.getWriter();
    	 out.println("Página não existe");
     }
 }